package cn.hicard.auth.web.util;

import java.util.List;

import cn.hicard.auth.entity.MgSource;
import cn.hicard.auth.web.Node;

public class WebUI {
	
	public static String buildMainMenu(List<MgSource> list){
		StringBuilder sb = new StringBuilder();
		sb.append("<ul>");
		for (MgSource source : list) {
			sb.append("<li>");
			sb.append("</a>");
			sb.append("</li>");
		}
		sb.append("</ul>");
		return sb.toString();
	}
	
	public static String buildSideMenu(){
		StringBuilder sb = new StringBuilder();
		
		return sb.toString();		
	}
		
	
	/**
	 * 利用树形数据结构，构建树形HTML，用于显示导航效果，没有选项框
	 * @param node
	 * @param leafClickHandler
	 * @param labelClickHandler
	 * @return
	 */
 	public static String buildNaviTree(Node node, String leafClickHandler, String labelClickHandler){
		StringBuilder sb = new StringBuilder();
		// 递归树，将树转为相应的HTML
		if (node != null) {
			sb.append("<div class='node_wrap'>");
			sb.append("<div class='node'>");
			
			List<Node> children = node.getChildren();			
			if(children != null && children.size()>0){
				sb.append("<img src='auth/image/nodeclose.png' onclick="+ leafClickHandler +"/>");	
			}else{
				sb.append("<img src='auth/image/nodeblank.png' onclick="+ leafClickHandler +"/>");
			}
			String span = "<span class='node_label' onclick="+labelClickHandler+">";
			sb.append(span.replace("{0}", ""+node.getPk()));
			sb.append(node.getName());
			sb.append("</span>");
			sb.append("</div>");
			
			sb.append("<div class='node_children'>");
			if (children != null) {
				for (Node child : children) {
					sb.append(buildNaviTree(child, leafClickHandler, labelClickHandler));
				}
			}
			sb.append("</div>");			
			
			sb.append("</div>");
		}
		return sb.toString();
	}
	
	
	/**
	 * 利用树形数据结构，构建树形HTML，包含Checkbox选择器
	 * @param node
	 * @param hasSelecter是否包含Checkbox选择器
	 * @param leafClickHandler节点树叶点击事件javascript代码
	 * @param selecterClickHandler节点选择框点击事件javascript代码
	 * @param labelClickHandler节点文本点击事件javascript代码
	 * @return
	 */
	public static String buildSelectTree(Node node, List<Integer> selectedPks, String leafClickHandler, String selecterClickHandler) {
		StringBuilder sb = new StringBuilder();
		// 递归树，将树转为相应的HTML
		if (node != null) {
			sb.append("<div class='node_wrap'>");
			sb.append("<div class='node'>");
			
			// 没有子节点就不要选择框
			List<Node> children = node.getChildren();
			
			// 判断子节点选中个数：0－未选，等于子节点个数－全选，否则半选
			int count = 0;
			boolean hasChild = (children != null && children.size()>0);
			if(hasChild){
				sb.append("<img src='auth/image/nodeclose.png' onclick="+ leafClickHandler +"/>");	
				for (Node child : children) {
					if(selectedPks.contains(child.getPk())){
						count++;
					}
				}
			}else{
				sb.append("<img src='auth/image/nodeblank.png' onclick="+ leafClickHandler +"/>");
			}
			
			// 全选：[有子节点，且全选] 或 [没有子节点，但自身选中]
			if(hasChild && count==children.size() || !hasChild && selectedPks.contains(node.getPk())){
				sb.append("<img pk='"+ node.getPk() +"' state='2' src='auth/image/fullselect.png' onclick="+selecterClickHandler+"/>");	
			}
			// 不选：[有子节点，但全没选] 或 [没有子节点，且自身也没选]
			else if(hasChild && count==0 || !hasChild && !selectedPks.contains(node.getPk())){
				sb.append("<img pk='"+ node.getPk() +"' state='0' src='auth/image/unselect.png' onclick="+selecterClickHandler+"/>");
			}
			// 半选
			else{
				sb.append("<img pk='"+ node.getPk() +"' state='0' src='auth/image/halfselect.png' onclick="+selecterClickHandler+"/>");
			}
			
			sb.append("<span class='node_label'>");
			sb.append(node.getName());
			sb.append("</span>");
			sb.append("</div>");
			
			sb.append("<div class='node_children'>");
			// 有子节点就递处理
			if (children != null) {
				for (Node child : children) {
					sb.append(buildSelectTree(child, selectedPks, leafClickHandler, selecterClickHandler));
				}
			}
			sb.append("</div>");
			
			
			sb.append("</div>");
		}
		return sb.toString();
	}
	
	
	/**
	 * 利用树形数据结构，构建树形HTML
	 * @param node
	 * @param hasSelecter是否包含Checkbox选择器
	 * @param leafClickHandler节点树叶点击事件javascript代码
	 * @param selecterClickHandler节点选择框点击事件javascript代码
	 * @param labelClickHandler节点文本点击事件javascript代码
	 * @return
	 */
	public static String buildTree(Node node, boolean hasSelecter, String leafClickHandler, String selecterClickHandler, String labelClickHandler) {
		StringBuilder sb = new StringBuilder();
		// 递归树，将树转为相应的HTML
		if (node != null) {
			sb.append("<div class='node_wrap'>");
			sb.append("<div class='node'>");
			
			List<Node> children = node.getChildren();			
			if(children != null && children.size()>0){
				sb.append("<img src='auth/image/nodeclose.png' onclick="+ leafClickHandler +"/>");	
			}else{
				sb.append("<img src='auth/image/nodeblank.png' onclick="+ leafClickHandler +"/>");
			}			
			if(hasSelecter){
				sb.append("<img pk='"+ node.getPk() +"' state='0' src='auth/image/unselect.png' onclick="+selecterClickHandler+"/>");
			}		
			String span = "<span class='node_label' onclick="+labelClickHandler+">";
			sb.append(span.replace("{0}", String.valueOf(node.getPk())));
			sb.append(node.getName());
			sb.append("</span>");
			sb.append("</div>");
			

			sb.append("<div class='node_children'>");
			if (children != null) {
				for (Node child : children) {
					sb.append(buildTree(child, hasSelecter, leafClickHandler, selecterClickHandler, labelClickHandler));
				}
			}
			sb.append("</div>");
			
			
			sb.append("</div>");
		}
		return sb.toString();
	}




}


